package com.junsi.web.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.junsi.common.annotation.Log;
import com.junsi.common.core.controller.BaseController;
import com.junsi.common.core.domain.AjaxResult;
import com.junsi.common.core.domain.Ztree;
import com.junsi.common.core.page.TableDataInfo;
import com.junsi.common.enums.BusinessType;
import com.junsi.common.utils.StringUtils;
import com.junsi.common.utils.poi.ExcelUtil;
import com.junsi.framework.util.ShiroUtils;
import com.junsi.system.domain.SysArea;
import com.junsi.system.service.ISysAreaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 地区 信息操作处理
 * 
 * @author ymx
 * @date 2019-05-29
 */
@Api("组织管理配置")
@Controller
@RequestMapping("/system/area")
public class SysAreaController extends BaseController
{
	private String prefix = "system/area";

	@Autowired
	private ISysAreaService areaService;

	@RequiresPermissions("system:area:view")
	@GetMapping()
	public String area()
	{
		return prefix + "/area";
	}

	/**
	 * 查询地区列表
	 */
	@ApiOperation("查询大区列表")
	@ApiImplicitParam(name = "area", value = "区域信息", dataType = "SysArea")
	@RequiresPermissions("system:area:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysArea sysarea,String areaCode)
	{
		startPage();
		//新增或编辑框展示大区数据
		if (null != areaCode) {
			return getDataTable(areaService.selectList(areaCode));
		}
		//列表大区数据
		return getDataTable(areaService.selectAreaList(sysarea));
	}

	/**
	 * 查询区域列表
	 */
	@ApiOperation("查询区域列表")
	@ApiImplicitParam(name = "code", value = "区域编码", dataType = "String")
	@RequiresPermissions("system:area:list")
	@PostMapping("/list/leve3")
	@ResponseBody
	public TableDataInfo leve3(String code)
	{
		startPage();
		List<SysArea> dataList = areaService.selectAreaByPar(code);
		return getDataTable(areaService.leve3(dataList, code));
	}


	/**
	 * 导出地区列表
	 */
	@ApiOperation("组织管理导出")
	@ApiImplicitParam(name = "area", value = "区域信息", dataType = "SysArea")
	@RequiresPermissions("system:area:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(SysArea area)
	{
		List<SysArea> list = areaService.selectAreaList(area);
		ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
		return util.exportExcel(list, "area");
	}

	/**
	 * 新增地区
	 */
	@ApiOperation("组织管理新增")
	@GetMapping("/add")
	public String add(String areaCode, ModelMap mmap)
	{
		//判断新增、修改
		if (null != areaCode) {
			SysArea area = areaService.selectAreaById(areaCode);
			mmap.put("area", area);
		}else {
			SysArea area = new SysArea();
			mmap.put("area", area);
		}
		return prefix + "/add";
	}

	/**
	 * 新增保存地区
	 */
	@ApiOperation("组织管理新增保存")
	@ApiImplicitParam(name = "area", value = "区域信息", dataType = "SysArea")
	@RequiresPermissions("system:area:add")
	@Log(title = "地区", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(String areaCode, SysArea area)
	{	
		area.setAreaName(StringUtils.replace(area.getAreaName(), "&amp;", "&"));
		area.setCreateBy(ShiroUtils.getLoginName());
		area.setUpdateBy(ShiroUtils.getLoginName());
		return areaService.addSave(areaCode,area);
	}

	/**
	 * 修改地区
	 */
	@ApiOperation("组织管理修改")
	@ApiImplicitParam(name = "areaCode", value = "区域编码", dataType = "String")
	@GetMapping("/edit/{areaCode}")
	public String edit(@PathVariable("areaCode") String areaCode, ModelMap mmap)
	{
		SysArea area = areaService.selectAreaById(areaCode);
		mmap.put("area", area);
		return prefix + "/edit";
	}

	/**
	 * 修改保存地区
	 */
	@ApiOperation("组织管理修改保存")
	@ApiImplicitParam(name = "area", value = "区域信息", dataType = "SysArea")
	@RequiresPermissions("system:area:edit")
	@Log(title = "地区", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SysArea area)
	{		
		return toAjax(areaService.updateArea(area));
	}

	/**
	 * 删除地区
	 */
	@ApiOperation("组织管理删除")
	@RequiresPermissions("system:area:remove")
	@ApiImplicitParam(name = "ids", value = "区域编码", dataType = "String")
	@Log(title = "地区", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{	
		try {
			return toAjax(areaService.deleteById(ids));
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	/**
	 * 选择区域树
	 */
	@GetMapping("/selectAreaTree")
	public String selectAreaTree()
	{
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData()
	{
		List<Ztree> ztrees = areaService.selectAreaTree(new SysArea());
		return ztrees;
	}


}
